 # --*-- coding:utf-8 --*--

import sys
sys.path.append("..")
from multiprocessing import Pool
from multiprocessing.dummy import Pool
import requests
import re
import datetime
import urllib
import warnings
warnings.filterwarnings("ignore")
import time
import random
# reload(sys)
# sys.setdefaultencoding("utf8")

from urllib.parse import quote



class Get_data(object):

	'''获取关键词在线商品数和成交数，竞争度 = 搜索人气/在线商品数'''
	get_cookie = None
	reserve_price = ''
	proxies = None
	
	def get_sale(self, keyword):
		url = 'https://s.m.taobao.com/search' # WX端数据
		head = {
				'Host': 's.m.taobao.com',
				'Connection': 'keep-alive',
				'Pragma': 'no-cache',
				'Cache-Control': 'no-cache',
				'Accept': 'application/json',
				'X-Requested-With': 'XMLHttpRequest',
				'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1',
				'Accept-Encoding': 'gzip, deflate, br',
				'Accept-Language': 'zh-CN,zh;q=0.9'
				}

		payload = {
				'event_submit_do_new_search_auction':'1',
				'_input_charset':'utf-8',
				'topSearch':'1',
				'atype':'b',
				'searchfrom':'1',
				'action':'home:redirect_app_action',
				'from':'1',
				'q':keyword,
				'sst':'1',
				'n':'20',
				'buying':'buyitnow',
				'm':'api4h5',
				'abtest':'6',
				'wlsort':'6',
				'style':'list',
				'closeModues':'nav,selecthot,onesearch',
				'sort':'_sale',
				'page':'1'
			}
		# 有价格区间的参数
		if self.reserve_price != '':  # 没有价格区间记得传''
			payload['start_price'] = self.reserve_price[0]
			payload['end_price'] = self.reserve_price[1]

		i = 0
		while True:
			resp = requests.get(url,params = payload,verify=False,proxies = self.proxies,headers = head)
			try:
				sale = resp.json()['listItem'][0]['act']
				return sale
			except:
				time.sleep(random.uniform(0.5,1))
				i = i+1
				if i > 3:
					return 0
			



	def get_onlineGoods(self,keyword):

		''' 通过关键词获取搜索榜数据'''

		# 获取在线商品数
		sale = self.get_sale(keyword)

		current_time = (str(datetime.datetime.now() - datetime.timedelta(days = 1)).split(' ')[0])
		pro_time = (str(datetime.datetime.now() - datetime.timedelta(days = 7)).split(' ')[0])


		aa = quote(keyword)
		k = 0
		url = 'https://sycm.taobao.com/mc/searchword/relatedWord.json?'
		# payload = {}
		# for i in aa :
		# 	url = url + str(k) + '=' + i + '&'
		# 	k = k + 1
		url = url + 'dateRange='+pro_time+'%7C'+current_time+'&dateType=recent7&pageSize=10&page=1&order=desc&orderBy=seIpvUvHits&keyword='+keyword+'&device=0&indexCode=seIpvUvHits%2CsePvIndex%2CclickRate%2CclickHits%2CclickHot&_=1539226143123&token='
		headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
				   'Host':'sycm.taobao.com',
				   'Referer': 'https://sycm.taobao.com/mq/words/search_words.htm?spm=a21ag.7782692.0.0.3dca2251R1F4qM',
				   'Accept': '*/*'
				   }

		time.sleep(random.uniform(1,30)) # 1-30秒随机请求生意参谋数据
		resp = requests.get(url,verify=False,headers = headers,cookies = self.get_cookie,proxies = self.proxies)
		print (resp.text)
		if resp.json()['code'] == 5810 :
			return resp.json()
		else:

			data_dict = resp.json()
			keyword_info = []
			if data_dict['data'] != []:
				for i in data_dict['data']:
					if i['keyword'] == keyword:
							i['sale'] = sale
							return i
			else:
				return {
		            "sePvIndex": 0,
		            "sale": sale,
		            "p4pAmt":0,
		            "clickHot": 0,
		            "onlineGoodsCnt": 0,
		            "payConvRate": 0,
		            "tmClickRatio": 0,
		            "clickRate":0,
		            "spvRatio":0,
		            "seIpvUvHits": 0,
		            "clickHits": 0,
		            "tradeIndex": 0,
		            "keyword": keyword
    }
				

### 测试

# 2018-6-4测试完成
if __name__ == '__main__':

	############
	### 滴滴优惠卷:像这种关键词是没有商品的，所以会取不到销量，只需要返回空值
	###########




	get_data = Get_data()
	get_data.get_cookie = {"v":"0","uc3":"vt3=F8dByR%2FMfjK%2BSMqvffc%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=UIHiLt3xD8xYTw%3D%3D","lgc":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","mt":"np=","cookie2":"15f460246ca265161500c145e028c0f1","tracknick":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","_tb_token_":"e6b39beb7338e","_l_g_":"Ug%3D%3D","cookie17":"UNN0nnQ5PzBj6g%3D%3D","skt":"bf03a9a56a8326d9","sg":"%E5%BA%9702","cookie1":"UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D","_cc_":"U%2BGCWk%2F7og%3D%3D","unb":"3397628220","JSESSIONID":"12C5D1C61382A10DD5C22FE451DED27B","existShop":"MTU0MjAxNDAwMA%3D%3D","csg":"8a1ff217","_nk_":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","isg":"BE9PloH3SK9pykzTFaq87vbb0upZdKOWCMdRvWFc677FMG8yaUQz5k2iNu6OU3sO","tg":"0","cna":"15ZiFBhnImkCAcFwhT7OdcJz","dnk":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","t":"9e8207e0ed67fd8f7d343ad3e63f299f","uc1":"cookie16=Vq8l%2BKCLySLZMFWHxqs8fwqnEw%3D%3D&tmb=1&cookie21=VFC%2FuZ9aj38X&cookie15=W5iHLLyFOGW7aA%3D%3D&existShop=true&pas=0&cookie14=UoTYNO8Q1DhKNQ%3D%3D&tag=8&lng=zh_CN"}
	# # 单线测试
	# keyword = '女包'
	# # get_data.reserve_price = [50,100]
	# print (get_data.get_sale(keyword))


	#多线测试
	keyword_list = ['#开动脑筋提升创造力#']
	
	# reserve_price = [50,100]
	# data_list = [keyword,reserve_price]
	pool = Pool(10)
	data = pool.map(get_data.get_onlineGoods,keyword_list)
	pool.close()
	pool.join()
	print (data)

